﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DNA.Mvc.Installation;

namespace DNA.Mvc.Publishing.Services
{
    public class BlogInstaller : InstallerBase
    {
        IPubContext _context;

        public BlogInstaller(IPubContext pubContext)
        {
            _context = pubContext;
        }

        public override string Name
        {
            get { return "blogs"; }
        }

        public override void Execute(InstallationContext context)
        {
            if (context.Data.ContainsKey("Web.Name"))
            {
                var name = context.Data["Web.Name"].ToString();
                if (_context.Categories.Count(c => c.Path.Equals(name, StringComparison.OrdinalIgnoreCase)) == 0)
                {
                    var category = new Category()
                    {
                        Title = name,
                        Path = name,
                        LastPosted = new DateTime(1800, 1, 1)
                    };
                    _context.Categories.Create(category);
                    _context.SaveChanges();
                }
            }
        }
    }
}
